package com.subject.shuati.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * 数据库加密
 */
public class DruidencryptUtil {
    private static String publicKye;
    private static String privateKey;

    private static String publicDomKey;

    private static String privateDomKey;

    static {
        try {
            String[] strings = ConfigTools.genKeyPair(512);
            privateKey = strings[0];
            System.out.println("privateKey="+privateKey);
            publicKye = strings[1];
            System.out.println("publicKye="+publicKye);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }
    public static String encrypt(String plainText) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey, plainText);
        System.out.println("encrypt="+encrypt);
        return encrypt;
    }
    public static String decrypt(String encryptText) throws Exception {
        String decrypt = ConfigTools.encrypt(publicKye, encryptText);
        System.out.println("decrypt="+decrypt);
        return decrypt;
    }

    static {
        try {
            String[] strings = ConfigTools.genKeyPair(512);
            privateDomKey = strings[0];
            System.out.println("privateDomKey="+privateDomKey);
            publicDomKey = strings[1];
            System.out.println("publicDomKey="+publicDomKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }
    public static String domEncrypt(String plainText) throws Exception {
        String encrypt = ConfigTools.encrypt(privateDomKey, plainText);
        System.out.println("domEncrypt="+encrypt);
        return encrypt;
    }
    public static String domDecrypt(String encryptText) throws Exception {
        String decrypt = ConfigTools.encrypt(publicDomKey, encryptText);
        System.out.println("domDecrypt="+decrypt);
        return decrypt;
    }

    public static void main(String[] args) throws Exception {
        String encrypt = encrypt("158786");
        System.out.println("encrypt=="+encrypt);
//        String encrypt = domEncrypt("192.168.189.130");
//        System.out.println("encrypt=="+encrypt);
    }
}
